package de.mp3player.gui.dialogs;

import java.awt.*;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.*;

import de.mp3player.app.App;

public class HelpDialog extends JPanel implements ControllerListener{
	
	private Player player;
	private JFrame parFrame;
	private Container cp;
	private Component visualComponent;
	private Component ctlComponent;
	private String mediaName;
	private URL url;
	
	public HelpDialog(JFrame pf,String media){
		parFrame = pf;
		mediaName = media;
		parFrame.setSize(300,300);
		parFrame.setVisible(true);
		cp = this;
		cp.setLayout(new BorderLayout());
		try {
			url = getClass().getResource(mediaName);
			player = Manager.createPlayer(url);
			player.addControllerListener(this);
		} catch (MalformedURLException e) {e.printStackTrace();}
		catch (NoPlayerException e) {e.printStackTrace();}
		catch (IOException e) {e.printStackTrace();}
		player.start();
	}
	public void stop(){
		if(player == null) return;
		player.stop();
		player.deallocate();
	}
	public void destroy(){
		if(player==null)return;
		player.close();
	}
	@Override
	public synchronized void controllerUpdate(ControllerEvent event) {
		if(event instanceof RealizeCompleteEvent){
			if((visualComponent = player.getVisualComponent()) != null)
				cp.add(BorderLayout.CENTER,visualComponent);
			if((ctlComponent = player.getControlPanelComponent()) != null)
				cp.add(BorderLayout.SOUTH,ctlComponent);
			if(parFrame != null){
				parFrame.pack();
				parFrame.setTitle(mediaName);
			}
		}
		
	}
}
